﻿//-----------------------------------------------------------------------
// <copyright file="UserOptions.cs" company="Sondre Bjellås">
// This software is licensed as Microsoft Public License (Ms-PL).
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Drawing;
using System.Windows.Forms;
using FlickrDownloadr.Services;

namespace FlickrDownloadr
{
    public partial class UserOptions : UserControl, IWizardPage
    {
        public UserOptions()
        {
            InitializeComponent();
        }

        public void ReloadPage()
        {
            
        }

        public event NextDelegate Next;

        public Bitmap Icon
        {
            get { return Properties.Resources.search; }
        }

        public string Title
        {
            get { return Properties.Resources.PageUserTitle; }
        }

        public string Description
        {
            get { return Properties.Resources.PageUserDescription; }
        }

        public void OnNext(bool refresh)
        {
            if (Next != null)
            {
                Next(this, refresh);
            }
        }

        private void ButtonNext_Click(object sender, EventArgs e)
        {
            string userName = textBox1.Text.Trim();
            PhotoService.Instance.SearchOptions.Text = userName;

            try
            {
                // Try and get the user ID.
                var user = PhotoService.Instance.FindUserId(userName);

                if (user == null)
                {
                    MessageBox.Show("Unable to find the selected user. Please try another name.", "Wrong User", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // Set the user ID on the search options.
                PhotoService.Instance.Options.UserId = user.UserId;

                //var user = FlickrFactory.GetInstance().PeopleFindByUsername(PhotoService.Instance.SearchOptions.Text);
                //PhotoService.Instance.SearchOptions.UserId = user.UserId;
            }
            catch (System.Net.WebException wex)
            {
                // Perhaps the firewall was turned on? Give the user a dialog to retry...

                DialogResult result = MessageBox.Show(Properties.Resources.ErrorNetworkAccess, Properties.Resources.ErrorNetworkAccessTitle, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);

                if (result == DialogResult.Retry)
                {
                    ButtonNext_Click(sender, e);
                }
                else
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("System error occurred.\r\n\r\n" + ex.ToString(), "System error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            OnNext(true);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void UserOptions_Load(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                e.Handled = true;
                buttonNext.PerformClick();
            }
        }

        private void radioButtonFavorites_CheckedChanged(object sender, EventArgs e)
        {
            
        }
    }
}
